<?php
/**
 *  date: 2017/7/10  11:03
 *  Author: Lin
 *  encoding:utf-8  language:PHP
 */
namespace Admin\Controller;
use Admin\Model\ServeModel;
use Admin\Model\AdminBaseModel;


class ServeController extends AdminBaseController
{
    protected $_serveObject;

    public function _initialize()
    {
        parent::_initialize();
        $this->_serveObject = new ServeModel();
    }


/**********************************************服务商start************************************************/
    /**
     *服务申请列表
     * @Date 201707
     * @Author Lin
     */
    public function proposerList()
    {
       $name =  I('facilitator_name');
       $mobile =  I('mobile');
       $status =  I('status');
       $type =  I('type');
        $data = $this->_serveObject->getproposerList($name,$mobile,$status,$type);

        //var_dump($data);die;
        $this->assign('data',$data[0]);
        $this->assign('page',$data[1]);
        $this->display();
    }

    /**
     *增加
     * @Date 201707
     * @Author Lin
     */
    public function proposerAdd()
    {
        $fpModel = M('facilitator_proposer');
        if (IS_POST)
        {
            //var_dump($_POST);die;
            if ($fpModel->create(I('post.'), 1))
            {
                $dataPost = I('post.');
                    $ret = uploadOne('image', 'Proposer', array(array(300, 300)));
                    if($ret['ok']==1)
                    {
                        $dataPost['image'] = $ret['images'][0];
                        $dataPost['smallimage'] = $ret['images'][1];
                    }
                    $dataPost['add_time'] = time();
                    //var_dump($dataPost);die;
                    if(false !== $fpModel->add($dataPost))
                    {
                        $this->success('增加成功');
                        exit;
                    }

            }
            $this->error($fpModel->getError());
        }
        $this -> display();
    }

    /**
     *服务商申请修改
     * @Date 201707
     * @Author Lin
     */
    public function proposerEdit()
    {
        $id = I('id');
        $fpModel = M('facilitator_proposer');
        if(IS_POST)
        {
            $dataPost = I('post.');
            if($fpModel->create($dataPost,2))
            {
                $ret = uploadOne('image', 'Proposer', array(array(300, 300)));

                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                if($dataPost['status'] !=3)
                {
                    unset($dataPost['reason']);
                }
                //var_dump($dataPost);die;
                if(false !== $fpModel->save($dataPost))
                {
                    $this->success('修改成功');
                    exit;
                }
            }
            $this->error($fpModel->getError());
        }
        $data = $fpModel->find($id);
        $this->assign('data',$data);
        $this->display();
    }
    /**********************************************服务商end************************************************/

    /**********************************************酒店start************************************************/
    /**
     *服务申请列表
     * @Date 201707
     * @Author Lin
     */
    public function hotelList()
    {
        $name =  I('hotel_name');
        $mobile =  I('mobile');
        $status =  I('status');
        $type =  I('type');
        $data = $this->_serveObject->gethotelList($name,$mobile,$status,$type);

        //var_dump($data);die;
        $this->assign('data',$data[0]);
        $this->assign('page',$data[1]);
        $this->display();
    }

    /**
     *增加题目
     * @Date 201707
     * @Author Lin
     */
    public function hotelAdd()
    {
        $fpModel = M('hotel');
        if (IS_POST)
        {
            //var_dump($_POST);die;
            if ($fpModel->create(I('post.'), 1))
            {
                $dataPost = I('post.');
                $ret = uploadOne('image', 'hotel', array(array(300, 300)));
                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                $dataPost['add_time'] = time();
                //var_dump($dataPost);die;
                if(false !== $fpModel->add($dataPost))
                {
                    $this->success('增加成功','hotelList',1);
                    exit;
                }

            }
            $this->error($fpModel->getError());
        }
        $this -> display();
    }

    /**
     *服务商申请修改
     * @Date 201707
     * @Author Lin
     */
    public function hotelEdit()
    {
        $id = I('id');
        $hModel = M('hotel');
        if(IS_POST)
        {
            $dataPost = I('post.');
            if($hModel->create($dataPost,2))
            {
                $ret = uploadOne('image', 'hotel', array(array(300, 300)));

                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                if($dataPost['status'] !=3)
                {
                    unset($dataPost['reason']);
                }
                //var_dump($dataPost);die;
                if(false !== $hModel->save($dataPost))
                {
                    $this->success('修改成功');
                    exit;
                }
            }
            $this->error($hModel->getError());
        }
        $data = $hModel->find($id);
        $this->assign('data',$data);
        $this->display();
    }
    /**********************************************酒店end************************************************/


    /**********************************************交通start************************************************/
    /**
     *服务申请列表
     * @Date 201707
     * @Author Lin
     */
    public function trafficList()
    {
        $name =  I('traffic_name');
        $mobile =  I('mobile');
        $status =  I('status');
        $data = $this->_serveObject->gettrafficList($name,$mobile,$status);

       // var_dump($data);die;
        $this->assign('data',$data[0]);
        $this->assign('page',$data[1]);
        $this->display();
    }

    /**
     *增加
     * @Date 201707
     * @Author Lin
     */
    public function trafficAdd()
    {
        $fpModel = M('traffic');
        if (IS_POST)
        {
            //var_dump($_POST);die;
            if ($fpModel->create(I('post.'), 1))
            {
                $dataPost = I('post.');
                $ret = uploadOne('image', 'traffic', array(array(300, 300)));
                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                $dataPost['add_time'] = time();
                //var_dump($dataPost);die;
                if(false !== $fpModel->add($dataPost))
                {
                    $this->success('增加成功','trafficList',1);
                    exit;
                }

            }
            $this->error($fpModel->getError());
        }
        $this -> display();
    }

    /**
     *服务商申请修改
     * @Date 201707
     * @Author Lin
     */
    public function trafficEdit()
    {
        $id = I('id');
        $hModel = M('traffic');
        if(IS_POST)
        {
            $dataPost = I('post.');
            if($hModel->create($dataPost,2))
            {
                $ret = uploadOne('image', 'traffic', array(array(300, 300)));

                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                if($dataPost['status'] !=3)
                {
                    unset($dataPost['reason']);
                }
                //var_dump($dataPost);die;
                if(false !== $hModel->save($dataPost))
                {
                    $this->success('修改成功','trafficList',1);
                    exit;
                }
            }
            $this->error($hModel->getError());
        }
        $data = $hModel->find($id);
        $this->assign('data',$data);
        $this->display();
    }
    /**********************************************酒店end************************************************/

    /**********************************************基地start************************************************/
    /**
     *服务申请列表
     * @Date 201707
     * @Author Lin
     */
    public function baseList()
    {
        $name =  I('base_name');
        $mobile =  I('mobile');
        $status =  I('status');
        $data = $this->_serveObject->getbaseList($name,$mobile,$status);

        //var_dump($data);die;
        $this->assign('data',$data[0]);
        $this->assign('page',$data[1]);
        $this->display();
    }

    /**
     *增加
     * @Date 201707
     * @Author Lin
     */
    public function baseAdd()
    {
        $fpModel = M('base');
        if (IS_POST)
        {
            //var_dump($_POST);die;
            if ($fpModel->create(I('post.'), 1))
            {
                $dataPost = I('post.');
                $ret = uploadOne('image', 'traffic', array(array(300, 300)));
                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                $dataPost['add_time'] = time();
                //var_dump($dataPost);die;
                if(false !== $fpModel->add($dataPost))
                {
                    $this->success('增加成功','baseList',1);
                    exit;
                }

            }
            $this->error($fpModel->getError());
        }
        $category = $this->_serveObject->getCategoryField();//分类
        $this->assign('category', $category);
        $this -> display();
    }

    /**
     *服务商申请修改
     * @Date 201707
     * @Author Lin
     */
    public function baseEdit()
    {
        $id = I('id');
        $hModel = M('base');
        if(IS_POST)
        {
            $dataPost = I('post.');
            if($hModel->create($dataPost,2))
            {
                $ret = uploadOne('image', 'base', array(array(300, 300)));
                if($ret['ok']==1)
                {
                    $dataPost['image'] = $ret['images'][0];
                    $dataPost['smallimage'] = $ret['images'][1];
                }
                if($dataPost['status'] !=3)
                {
                    unset($dataPost['reason']);
                }
                //var_dump($dataPost);die;
                if(false !== $hModel->save($dataPost))
                {
                    $this->success('修改成功','baseList',1);
                    exit;
                }
            }
            $this->error($hModel->getError());
        }
        $data = $hModel->find($id);
        $category = $this->_serveObject->getCategoryField();//分类
        $this->assign('category', $category);
        $this->assign('data',$data);
        $this->display();
    }
    /**********************************************基地end************************************************/

    /**
     *酒店服务用户信息
     * @Date 201707
     * @Author Lin
     */
    public function userHotelList()
    {
        $mobile =  I('mobile');
        $data = $this->_serveObject->getUserHotelList($mobile);

        //var_dump($data);die;
        $this->assign('data',$data[0]);
        $this->assign('page',$data[1]);
        $this->display();

    }

    public function userHotelEdit()
    {
        $id = I('id');
        $hModel = M('user_hotel');
        if(IS_POST)
        {
            $dataPost = I('post.');
            if($hModel->create($dataPost,2))
            {
                //var_dump($dataPost);die;
                if(false !== $hModel->save($dataPost))
                {
                    $this->success('修改成功','userHotelList',1);
                    exit;
                }
            }
            $this->error($hModel->getError());
        }
        $data = $hModel->find($id);
        $this->assign('data',$data);
        $this->display();
    }


    /**
     *基地服务用户信息
     * @Date 201707
     * @Author Lin
     */
    public function userBaseList()
    {
        $mobile =  I('mobile');
        $data = $this->_serveObject->getUserBaselList($mobile);

        //var_dump($data);die;
        $this->assign('data',$data[0]);
        $this->assign('page',$data[1]);
        $this->display();

    }

    public function userBaseEdit()
    {
        $id = I('id');
        $hModel = M('user_base');
        if(IS_POST)
        {
            $dataPost = I('post.');
            $dataPost['time'] = strtotime($dataPost['time']);
            //var_dump($dataPost);die;
            if($hModel->create($dataPost,2))
            {
                //var_dump($dataPost);die;
                if(false !== $hModel->save($dataPost))
                {
                    $this->success('修改成功','userBaseList',1);
                    exit;
                }
            }
            $this->error($hModel->getError());
        }
        $data = $hModel->find($id);
        $this->assign('data',$data);
        $this->display();
    }




    /**
     *状态展示
     * @Date 201707
     * @Author Lin
     */
    public function statusEdit()
    {
        $Id = I('Id');
        $status = I('status');
        $model = I('model');
        //var_dump($menuId);var_dump($status);die;

        $result = M($model)->where(array('id'=>$Id))->setField('status',$status);

        if ($result !== false)
            $this->ajaxReturn(array('status'=>1));
        else
            $this->ajaxReturn(array('status'=>0));
    }

    /**
     * 单条删除
     */
    public function dataDel()
    {
        if(!IS_AJAX) return;
        $model = I('model');
        $result = M($model)-> delete(intval($_GET['id']));
        if($result)
            $this -> ajaxReturn(array('status' => 1,'message' => '删除成功'));
        else
            $this -> ajaxReturn(array('status' => 2,'message' => '操作失败'));
    }
    /**
     *全选删除
     * @Date 201703
     * @Author Linsenquan
     */
    public function deleteAll()
    {
        //var_dump($_POST);die;
        if(!IS_POST)
        $this->error('非法操作');
        if(empty($_POST))
            $this->error('请选择要删除的信息！','',1);
       //var_dump($_POST);die;
        $result = $this->_serveObject->deleteAllData($_POST);
        if($result)
            $this->success('删除成功','',1);
        else
            $this->error('删除失败','',1);
    }
    //  hotelList  trafficList




    /**
     * 分类列表
     */
    public function categoryList() {
        $data = $this->_serveObject->getCategory(I('name'));
        $this->assign('category', $data[0]);
        $this->assign('page', $data[1]);
        $this->display('category_index');
    }

    /**
     * 添加分类
     */
    public function addCategory() {
        if(IS_GET){
            $id = I('id');
            $act = I('get.act');
            if($id && $act == 'edit'){
                $data = $this->_serveObject->getCategoryFind($id);
                $data['describe'] = htmlspecialchars_decode($data['describe']);
                $this->assign('id', $id);
                $this->assign('category', $data);
            }
            $this->assign('act', $act);
            $this->display('add_category');
        }else if(IS_POST){
            $data = I('post.');
            $data['add_time'] = time();
            $admin_base_model = new AdminBaseModel('base_category');
            $act = I('post.act');
            if($act == 'add'){
                $result = $admin_base_model->addData($data);
                if($result)
                    $this->success('添加成功', U('categoryList'), 2);
                else
                    $this->error('操作失败', '', 2);
            }elseif($act == 'edit'){
                $id = I('id');
                if($this->_serveObject->editCategory($id, $data) !== false)
                    $this->success('修改成功', U('categoryList'), 2);
                else
                    $this->error('操作失败', '', 2);
            }
        }
    }

    /**
     * 删除分类
     */
    public function deleteCategory() {
        if(!IS_AJAX) return;

            $result = $this->_serveObject->deleteCategory(intval($_GET['id']));
            if($result)
                $this->ajaxReturn(array('status' => 1,'message' => '删除成功'));
            else
                $this->ajaxReturn(array('status' => 2,'message' => '操作失败'));

    }

    /**
     * 全选删除分类
     */
    public function deleteAllCategory(){
        if(!IS_POST) return;
        $result = $this->_serveObject->deleteAllCategory($_POST);
        if($result)
            $this->success('删除成功', U('categoryList'), 2);
        else
            $this->error('操作失败', '', 2);
    }

    /**
     * 操作
     */
    public function operate_category(){
        $operate = I('post.operate');
        $selected_id_arr = I('post.category_id');
        if(!in_array($operate,array('del','show','hide')) || !is_array($selected_id_arr)){
            $this->error('非法操作');
        }
        $map['id'] = array('in', $selected_id_arr);
        $base_category = M('base_category');
        switch ($operate){
            case 'del':
                $row = $base_category->where($map)->delete();
                break;
            case 'show':
                $data = array('is_show'=>1);
                $row = $base_category->where($map)->save($data);
                break;
            case 'hide':
                $data = array('is_show'=>2);
                $row = $base_category->where($map)->save($data);
                break;
        }
        if($row !== false)
            $this->success('操作成功', U('categoryList'), 1);
        else
            $this->error('操作失败');

    }

}